package com.zx.tradeclient.ctrl.index5;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.zx.base.crop.Crop;
import com.zx.base.ctrl.BaseActivity;
import com.zx.base.ctrl.Constant;
import com.zx.base.http.HttpUtils;
import com.zx.base.http.HttpUtils.CallBack;
import com.zx.base.util.URLHelper;
import com.zx.tradeclient.R;
import com.zx.tradeclient.app.TradeApp;

public class CompanyRewardInfo {

	private TradeApp app;
	private EditText editText;
	private ImageView imageView;
	private Context context;
	private ProgressDialog progressDialog;

	public CompanyRewardInfo(final Context context, View view, final Crop crop) {
		this.context = context;
		app = (TradeApp) context.getApplicationContext();
		imageView = (ImageView) view.findViewById(R.id.user_license_imageView);
		crop.setImageView(imageView);
		
		editText = (EditText) view.findViewById(R.id.user_license_editText);

		LinearLayout layout = (LinearLayout) view.findViewById(R.id.user_license_uploadImage);
		layout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				crop.showDialog();
			}
		});

		// 读取信息
		Map<String, String> map = new HashMap<String, String>();
		map.put("act", "aptitude");
		map.put("id", app.user.getId());
		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				try {
					String succeed = response.getString("succeed");
					if (succeed.equals("000")) {
						editText.setText(response.getString("aptitudeTitle"));
						app.imageLoader.DisplayImage(URLHelper.mPrefixImg + response.getString("aptitudeImg"), imageView, R.drawable.icon_upload);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}

			}

			@Override
			public void onFailure(Throwable e) {
				// TODO Auto-generated method stub

			}
		});
	}

	public void init() {
		((BaseActivity) context).header_right.setOnClickListener(new OnClickListener() {

			@SuppressWarnings("unchecked")
			@Override
			public void onClick(View v) {
				progressDialog = ProgressDialog.show(context, null, "正在提交数据数据，请稍后.....", true, true);
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("act", "aptitudeModify");
				params.put("id", app.user.getId());
				params.put("title", editText.getText().toString());
				Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
				params.put("photo", bitmap);
				new UploadImage().execute(params);	
			}
		});
	}

	class UploadImage extends AsyncTask<Map<String, Object>, Void, String> {

		@Override
		protected String doInBackground(Map<String, Object>... params) {
			return HttpUtils.postImage(params[0]);
		}

		@Override
		protected void onPostExecute(String result) {
			if(progressDialog!=null){
				progressDialog.dismiss();
				progressDialog=null;
			}
			Toast toast = Toast.makeText(context, "数据提交成功。", Toast.LENGTH_SHORT);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
			if(Constant.debug)Log.d("debug", result);
		}

		@Override
		protected void onCancelled() {
			if(Constant.debug)Log.d("debug", "onCancelled");
		}

	}
}
